import request from '@/utils/request'

// 获取剧本列表
export function getDramaList(params) {
  return request({
    url: '/evox-drama/drama/list',
    method: 'get',
    params
  })
}

// 获取剧本详情
export function getDramaDetail(id) {
  return request({
    url: `/evox-drama/drama/detail/${id}`,
    method: 'get'
  })
}

// 根据ID获取剧本详情(新接口)
export function getDramaById(id) {
  //todo 数字id丢失精度
  return request({
    url: `/evox-drama/drama/drama-id/${id}`,
    method: 'get'
  })
}

// 获取已确认的视频列表
export function confirmedVideoList(seriesId) {
  return request({
    url: `/evox-drama/storyboard/confirmed-videos`,
    method: 'get',
    params: { seriesId }
  })
}

// 创建剧本
export function createDrama(data) {
  return request({
    url: '/evox-drama/drama/create',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 更新剧本
export function updateDrama(data) {
  return request({
    url: '/evox-drama/drama/update',
    method: 'put',
    data
  })
}

// 删除剧本
export function deleteDrama(id) {
  return request({
    url: `/evox-drama/drama/drama-id/${id}`,
    method: 'delete'
  })
}

// 发布剧本
export function publishDrama(id) {
  return request({
    url: `/evox-drama/drama/publish/${id}`,
    method: 'put'
  })
}

// 取消发布剧本
export function unpublishDrama(id) {
  return request({
    url: `/evox-drama/drama/unpublish/${id}`,
    method: 'put'
  })
}

// 获取分集列表
export function getEpisodeList(dramaId) {
  return request({
    url: `/evox-drama/drama/episodes/${dramaId}`,
    method: 'get'
  })
}

// 获取剧本分集列表（新接口）
export function getDramaSeries(dramaId) {
  return request({
    url: `/evox-drama/series/drama/${dramaId}`,
    method: 'get'
  })
}

// 更新分集
export function updateEpisode(dramaId, episodeId, data) {
  return request({
    url: `/evox-drama/drama/episodes/${dramaId}/${episodeId}`,
    method: 'put',
    data
  })
}

// 创建分集
export function createEpisode(dramaId, data) {
  return request({
    url: `/evox-drama/drama/episodes/${dramaId}`,
    method: 'post',
    data
  })
}

// 删除分集
export function deleteEpisode(dramaId, episodeId) {
  return request({
    url: `/evox-drama/drama/episodes/${dramaId}/${episodeId}`,
    method: 'delete'
  })
}

// 获取剧本演员
export function getDramaActor(dramaId) {
  return request({
    url: `/evox-drama/actor/list/${dramaId}`,
    method: 'get'
  })
}

// 创建角色
export function createActor(data) {
  return request({
    url: '/evox-drama/actor/v2/create',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 更新角色
export function updateActor(data) {
  return request({
    url: '/evox-drama/actor/update',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 删除角色
export function deleteActor(actorId) {
  return request({
    url: `/evox-drama/actor/${actorId}`,
    method: 'delete'
  })
}

// 获取剧本场景
export function getDramaScene(dramaId) {
  return request({
    url: `/evox-drama/scene/list?dramaId=${dramaId}`,
    method: 'get'
  })
}

// 创建场景
export function createScene(data) {
  return request({
    url: '/evox-drama/scene/create',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

export function createSceneByAI(data) {
  return request({
    url: '/evox-drama/scene/create-ai',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 更新场景
export function updateScene(data) {
  return request({
    url: '/evox-drama/scene/v2/update',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 获取分镜列表
export function getDramaStoryboard(dramaId, params) {
  return request({
    url: `/evox-drama/storyboard/list/${dramaId}/page`,
    method: 'get',
    params
  })
}

export function initAllStoryboard(dramaId) {
  return request({
    url: `/evox-drama/storyboard/init-all/${dramaId}`,
    method: 'post'
  })
}

// 预初始化分镜
export function preInitStoryboard(dramaId, seriesId) {
  return request({
    url: `/evox-drama/storyboard/preInit/${dramaId}/${seriesId}`,
    method: 'post'
  })
}

// 确认保存分镜列表
export function confirmSaveStoryboard(dramaId, seriesId, data) {
  return request({
    url: `/evox-drama/storyboard/confirmSave/${dramaId}/${seriesId}`,
    method: 'post',
    data
  })
}

// 保存或更新分镜
export function saveOrUpdateStoryboard(dramaId, seriesId, data) {
  return request({
    url: `/evox-drama/storyboard/saveOrUpdate/${dramaId}/${seriesId}`,
    method: 'post',
    data
  })
}

// 查询分镜生成的图片列表
export function queryStoryboardImageList(dramaId, sbId) {
  return request({
    url: `/evox-drama/storyboard/query-choose-image-list/${dramaId}/${sbId}`,
    method: 'get'
  })
}

// 查询分镜生成的图片列表
export function queryStoryboardVideoList(dramaId, sbId) {
  return request({
    url: `/evox-drama/storyboard/query-choose-video-list/${dramaId}/${sbId}`,
    method: 'get'
  })
}

// 生成分镜图片
export function generateStoryboardImage(dramaId, sbId) {
  return request({
    url: `/evox-drama/storyboard/generate-image/${dramaId}/${sbId}`,
    method: 'post'
  })
}

// 初始化场景列表
export function initSceneList(dramaId) {
  return request({
    url: `/evox-drama/scene/init?dramaId=${dramaId}`,
    method: 'post'
  })
}

// 根据场景描述生成场景图
export function generateSceneImage(data) {
  return request({
    url: '/evox-drama/scene/generate-image',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 初始化剧集列表
export function initSeriesList(dramaId) {
  return request({
    url: `/evox-drama/series/initialize/${dramaId}`,
    method: 'post'
  })
}

// 文件上传接口
export function uploadFile(file) {
  const formData = new FormData()
  formData.append('file', file)
  console.log(formData.get('file'))
  return request({
    url: '/evox-drama/file/upload',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 确认分镜
export function confirmStoryboard(data) {
  return request({
    url: '/evox-drama/storyboard/confirm',
    method: 'post',
    data
  })
}

// 确认分镜图片
export function confirmStoryboardImage(data) {
  return request({
    url: '/evox-drama/storyboard/confirm-storyboard-image',
    method: 'post',
    data
  })
}

// 确认分镜图片
export function confirmStoryboardVideo(data) {
  return request({
    url: '/evox-drama/storyboard/confirm-storyboard-video',
    method: 'post',
    data
  })
}

// 删除分镜
export function deleteStoryboard(sbId) {
  return request({
    url: `/evox-drama/storyboard/delete/${sbId}`,
    method: 'delete'
  })
}

// 获取已确认的分镜图片列表
export function getConfirmedStoryboardImages(seriesId) {
  return request({
    url: `/evox-drama/storyboard/confirmed-images`,
    method: 'get',
    params: { seriesId }
  })
}

// 导出分镜图
export function exportStoryboardImages(dramaId, seriesId) {
  return request({
    url: `/evox-drama/storyboard/export-images/${dramaId}/${seriesId}`,
    method: 'post'
  })
}
// 导出分镜图
export function exportStoryboardVideo(dramaId, seriesId, sbIdList) {
  return request({
    url: `/evox-drama/storyboard/export-videos`,
    method: 'post',
    data: {
      dramaId,
      seriesId,
      sbIdList
    }
  })
}

// 导出分镜图再次
export function reExportStoryboardImagesAgain(dramaId, seriesId) {
  return request({
    url: `/evox-drama/storyboard/export-images/${dramaId}/${seriesId}`,
    method: 'post'
  })
}

// 获取已确认的分镜图片列表
export function getPutUrl(format) {
  return request({
    url: `/evox-drama/file/put-url`,
    method: 'get',
    params: { format }
  })
}

export async function putUrlToOss(url, file) {
  await fetch(url, {
    method: 'PUT',
    headers: {
      'Accept': '*/*',
      'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
      'Connection': 'keep-alive',
      'Content-Type': 'application/octet-stream',
      'DNT': '1',
      'Origin': 'null',
      'Sec-Fetch-Dest': 'empty',
      'Sec-Fetch-Mode': 'cors',
      'Sec-Fetch-Site': 'cross-site',
      'x-oss-meta-source': 'binary_data',
      'x-oss-storage-class': 'Standard'
    },
    body: file,
  });
}

// 检查场景状态
export function checkSceneStatus(dramaId) {
  return request({
    url: `/evox-drama/scene/init-status`,
    method: 'get',
    params: { dramaId }
  })
}

// 检查分镜状态
export function checkStoryboardStatus(dramaId) {
  return request({
    url: `/evox-drama/storyboard/init-status`,
    method: 'get',
    params: { dramaId }
  })
}

export function getCompleteCbList(dramaId) {
  return request({
    url: `/evox-drama/series/complete-sb/list/${dramaId}`,
    method: 'get'
  })
}

export function generateSceneBySeries(dramaId, seriesIdList) {
  return request({
    url: `/evox-drama/scene/generate-by-storyboard`,
    method: 'post',
    data: {
      dramaId,
      seriesIdList
    }
  })
}

export function generateVideo(data) {
  return request({
    url: `/evox-drama/storyboard/generate-video-draft`,
    method: 'post',
    data
  })
}

// 获取剧本权限列表
export function getDramaPermissions(dramaId) {
  return request({
    url: `/evox-drama/permission/drama/${dramaId}`,
    method: 'get'
  })
}

// 授权权限
export function grantPermission(data) {
  return request({
    url: '/evox-drama/permission/batch-grant',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 撤销权限
export function revokePermission(params) {
  return request({
    url: '/evox-drama/permission/revoke',
    method: 'delete',
    params
  })
}

// 搜索用户
export function searchUsers(params) {
  return request({
    url: '/evox-drama/user/search',
    method: 'get',
    params
  })
}

// 图片重绘
export function imageMaskEdit(data) {
  return request({
    url: `/evox-drama/image/mask/edit`,
    method: 'post',
    data
  })
}

// 智能选区
export function smartSelection(data) {
  return request({
    url: `/evox-drama/image/mask/generate`,
    method: 'post',
    data
  })
}

// 多主体编辑
export function multiSubjectEdit(data) {
  return request({
    url: `/evox-drama/image/prompt/edit`,
    method: 'post',
    data
  })
}

// 图片擦除
export function imageRemoveEdit(data) {
  return request({
    url: `/evox-drama/image/remove/edit`,
    method: 'post',
    data
  })
}

// 图片生成记录
export function imageGenerationRecord(params) {
  return request({
    url: `/evox-drama/user-assets/list/page`,
    method: 'get',
    params
  })
}

// 更新剧集内容
export function updateSeries(data) {
  return request({
    url: `/evox-drama/series/content/update`,
    method: 'post',
    data
  })
}

// 添加剧集
export function addSeries(data) {
  return request({
    url: `/evox-drama/series/create`,
    method: 'post',
    data
  })
}

// 重新拆分
export function reSplitSeries(dramaId, seriesId) {
  return request({
    url: `/evox-drama/storyboard/init/${dramaId}/${seriesId}`,
    method: 'post'
  })
}